package toOffer;

/**
 * @author YuanAo
 * @date 2024/10/18 15:47
 * @description 二叉树层序遍历
 */

import com.sun.org.apache.regexp.internal.RE;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * 给你二叉树的根节点 root ，返回其节点值的 层序遍历 。 （即逐层地，从左到右访问所有节点）。
 */
public class No102 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "[1, 2, 3]";

    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> lists = new ArrayList<>();
        LinkedList<TreeNode> nodes = new LinkedList<>();
        if (root != null){
            nodes.offer(root);
        }
        ArrayList<Integer> list = new ArrayList<>();
        while (!nodes.isEmpty()){
            TreeNode node = nodes.poll();
            list.add(node.val);
            if (node.left != null) nodes.add(node.left);
            if (node.right != null) nodes.add(node.right);
        }

        return lists;
    }

    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
     }
  }
}
